package com.android.tea.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.tea.FriendAddActivity;
import com.android.tea.R;
import com.android.tea.entity.NotifyInfo;
import com.android.tea.image.ImageLoaderManager;
import com.android.tea.util.StringUtils;
import com.android.tea.util.TeaLog;
import com.android.tea.util.Utils;

public class FriendsRequestAdapter extends BaseAdapter {

	private static final String TAG = "FriendsRequestAdapter";
	private LayoutInflater inflater;
	private List<NotifyInfo> list;
	private Context context;
	private ImageLoaderManager imageLoaderManager;
	
	public FriendsRequestAdapter(Context context,
			List<NotifyInfo> list) {
		this.context = context;
		this.list = list;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoaderManager = new ImageLoaderManager(context, new Handler(), this);

	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public NotifyInfo getItem(int position) {
		if (getCount() >= position + 1) {
			return list.get(position);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		NotifyInfo info = list.get(position);
		if (info == null) {
			return null;
		}
		final ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.friend_request_item, null);
			holder = new ViewHolder();
			holder.item = (RelativeLayout) convertView.findViewById(R.id.relativeLayout2);
			holder.ivAvatar = (ImageView) convertView.findViewById(R.id.avatar);
			holder.tvTitle = (TextView) convertView.findViewById(R.id.title);
			holder.tvDesc = (TextView) convertView.findViewById(R.id.desc);
			holder.tvTime = (TextView) convertView.findViewById(R.id.timestamp);
			holder.id = position;
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		OnClickListener clickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.relativeLayout2:
					TeaLog.i(TAG, "-------------");
					Intent intent = new Intent(context,FriendAddActivity.class);
					intent.putExtra("", "");//TODO 传参
					context.startActivity(intent);
					
					break;
				case R.id.avatar:
					
					break;
				default:
					break;
				}
			}

			
		};
		
		holder.item.setOnClickListener(clickListener);
		holder.ivAvatar.setOnClickListener(clickListener);
		
		setData(info,holder);
		
		return convertView;
	}
	static class ViewHolder {
		int id;
		RelativeLayout item;
		ImageView ivAvatar;
		TextView tvTitle;
		TextView tvDesc;
		TextView tvTime;
	}
	
	private void setData(NotifyInfo info, ViewHolder holder) {
		String title = info.getTitle();
		String avatar = info.getAvatar();
		String desc = info.getDesc();
		long timeStamp = info.getTimeStamp();
		
		if (StringUtils.isNotNullOrEmpty(avatar)) {
			holder.ivAvatar.setImageBitmap(imageLoaderManager.getImage(avatar, R.drawable.tea_default,
					Utils.dipToPixels(context, 74)));
		} else {
			holder.ivAvatar.setImageResource(R.drawable.tea_default);
		}
		
		if (StringUtils.isNotNullOrEmpty(title)) {
			holder.tvTitle.setText(title);
		}else {
			holder.tvTitle.setText(context.getResources().getString(R.string.unknown));
		}
		
		if (StringUtils.isNotNullOrEmpty(desc)) {
			holder.tvDesc.setText(desc);
		}else {
			holder.tvDesc.setText(context.getResources().getString(R.string.unknown));
		}
		
		if (timeStamp > 0) {
			holder.tvTime.setText(StringUtils.getFormatTime(timeStamp));
		}else {
			holder.tvTime.setText(context.getResources().getString(R.string.unknown));
		}
	}

}
